<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-01-31
 * Time: 14:27
 */

namespace app\admin\model;
use think\Loader;
use think\Model;

class Tag extends Model
{
    //设置当前模型对应的完整数据表名称
    protected $table = 'tp_tag';
    //设置关键字段
    protected $pk = 'tag_id';

    //添加标签
    public function addTage($data){

        $validate = Loader::validate('tag');
        if(!$validate->check($data)){
            return ['valid'=>0,'msg'=>$validate->getError()];
        }

        if($this->save($data)){

            return ['valid'=>1,'msg'=>'添加成功'];
        }else{

            return ['valid'=>0,'msg'=>'添加失败'];
        }

    }

    //删除标签
    public function del($tag_id){

        if(tag::destroy(['tag_id' => $tag_id])){

            return ['valid'=>1,'msg'=>'删除成功'];
        }else{

            return ['valid'=>0,'msg'=>'删除失败'];
        }
    }

    //编辑标签
    public function edits($data){

        if(!$this->validate(true)->save($data,[$this->pk=>$data['tag_id']])){

            return ['valid'=>0,'msg'=>$this->getError()];
        }

        return ['valid'=>1,'msg'=>'编辑成功'];
    }
}